package com.my.screenadapter.mdir;

import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.ajie.lib_base.ft_file.FileIOUtils;
import com.my.screenadapter.Base.BaseActivity;
import com.my.screenadapter.R;

import org.apache.log4j.Logger;

import java.io.File;

import butterknife.BindView;
import butterknife.ButterKnife;

public class DirActivity extends BaseActivity {

    Logger logger = Logger.getLogger(DirActivity.class);
    @BindView(R.id.tv_info)
    TextView tvInfo;
    @BindView(R.id.btn_save_file)
    Button btnSaveFile;
    @BindView(R.id.btn_deal_file)
    Button btnDealFile;

    @Override
    protected void init() {
        StringBuffer stringBuffer = new StringBuffer();

        //SD卡是否可用
        boolean mountSDCard = Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState());
        logger.info("app isSdcardMounted==" + mountSDCard);
        stringBuffer.append("app isSdcardMounted==" + mountSDCard);
        stringBuffer.append("\n\t");

        // TODO: 外部存储  SD卡或者系统自身有分配外部存储空间
        //android.permission.WRITE_EXTERNAL_STORAGE
        //SD根目录:/mnt/sdcard/ (6.0后写入需要用户授权)
        File sdCard = Environment.getExternalStorageDirectory();
        logger.info("getExternalStorageDirectory==" + sdCard.getAbsolutePath());///storage/emulated/0
        stringBuffer.append("getExternalStorageDirectory==" + sdCard.getAbsolutePath());
        stringBuffer.append("\n\t");

        //不需要用户授权权限  只需要 AndroidManifest.xml配置权限
        // /Android/data/< package name >/files/… (该路径通常挂载在/mnt/sdcard/下)
        File abc = getExternalFilesDir("abc");
        logger.info("getExternalFilesDir==" + abc.getAbsolutePath());///storage/emulated/0/Android/data/com.my.screenadapter/files/abc
        stringBuffer.append("getExternalFilesDir==" + abc.getAbsolutePath());
        stringBuffer.append("\n\t");

        // TODO: 内部存储  /data/data/< package name >/files/…
        File cacheDir = getCacheDir();
        logger.info("getCacheDir==" + cacheDir.getAbsolutePath());///data/data/com.my.screenadapter/cache
        stringBuffer.append("getCacheDir==" + cacheDir.getAbsolutePath());
        stringBuffer.append("\n\t");

        File filesDir = getFilesDir();
        logger.info("getFilesDir==" + filesDir.getAbsolutePath());///data/data/com.my.screenadapter/files
        stringBuffer.append("getFilesDir==" + filesDir.getAbsolutePath());
        stringBuffer.append("\n\t");

        tvInfo.setText(stringBuffer.toString());

    }

    @Override
    protected int initLayoutId() {
        return R.layout.activity_dir;
    }

    //保存
    public void saveFile(View view) {
        FileIOUtils.writeFileFromBytesByStream(new File(getFilesDir().getAbsolutePath()+File.separator+"abc.txt"),"I am my baby's Father".getBytes());

    }

    //删除
    public void dealFile(View view) {
        FileUtils.delFileIgnore(getFilesDir().getAbsolutePath(),"abc","abc.txt","abcd.txt");
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }


    String mTestDir;
    public void saveFileForNextStep(View view) {
        File filesDir = getFilesDir();
        mTestDir = filesDir.getAbsolutePath() + File.separator + "mTestDir";
        File file = new File(mTestDir);
        file.mkdirs();

        MFileUtils.appendFileBufferWtiter(mTestDir+File.separator+"abc","abc");
        MFileUtils.appendFileRandomAccess(mTestDir+File.separator+"bcd","bcd");
        Log.i(TAG, "saveFileForNextStep: path:"+filesDir.getAbsolutePath());
    }


    public void dealFile_beeboxes_focusdel(View view) {
        MFileUtils.delFilesExcludePathFolder(mTestDir);
    }

    public void dealFile_all(View view) {
        MFileUtils.delAll(mTestDir);
    }
}
